<?php

namespace App\Http\Controllers\Admin;

use App\Http\Common\QiniuUpload;
use App\Models\Article;
use App\Models\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $articles = Article::with('hasOneCategory')->orderBy('article_id','DESC')->paginate();
        return view('article.index',compact('articles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $category = Category::where('is_use' , 1)->get();
        return view('article.create',compact('category'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Requests\ArticleRequest $request)
    {
        //
        $file = $request->file('cover');

        $upload = new QiniuUpload();
        $res = $upload->upload($file);
        $cover = QiniuUpload::url . $res['key'];

        $input = Input::all();
        $input['cover'] = $cover;
        Article::create($input);

        return redirect()->route('admin.article.index')->with('success','Article created successfully');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $article = Article::with('hasOneCategory')->where('article_id',$id)->first();

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $category = Category::where('is_use' , 1)->get();
        $article = Article::with('hasOneCategory')->where('article_id',$id)->first();
        return view('article.edit',compact('article','category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Article::destroy($id);
        return redirect()->route('admin.article.index')->with('success','Article delete successfully');
    }

    /**
     * upload Qiniu Server
     */
    public function uploadServer(Request $request)
    {
        $files = $request->allFiles();

        $upload = new QiniuUpload();
        foreach($files as $file)
        {
            $res = $upload->upload($file);
            $cover = QiniuUpload::url . $res['key'];
        }

        $data = [
            'errno' => 0,
            'data'  => [
                '0'  => $cover,
            ]
        ];

        return json_encode($data);
    }
}
